package XZK10301002;

import java.util.*;

/**
 * <p>
 * description
 * </p>
 *
 * @author bing.z 2021/6/7 16:15
 **/
public class Demo {
    public static void main(String[] args) {
        /*ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("D");
        list.add("A");
        int index = listTest(list, "D");
        System.out.println(index);*/

        String[] strs = {"10001", "10086", "12347806666", "45612378901", "10001", "12347806666"};
        removeRepeatElem(strs);

        testComparableAndComparator();
    }

    /**
     * 3、分别用Comparable和Comparator两个接口对下列四位同学的成绩做降
     * 序排序，如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序。
     */
    private static void testComparableAndComparator() {
        Student a = new Student("贾宝玉", 14, 88.5);
        Student b = new Student("林黛玉", 13, 90.5);
        Student c = new Student("史湘云", 12, 90.5);
        Student d = new Student("薛宝钗", 15, 91);
        List<Student> list = new ArrayList<>();
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        /*Collections.sort(list);*/
        Collections.sort(list, (x, y) -> {
            if (x.getScore() != y.getScore()) {
                return Double.compare(x.getScore(), y.getScore());
            }
            return -Integer.compare(x.getAge(), y.getAge());
        });
        for (Student s : list) {
            System.out.println(s);
        }
    }

    static class Student implements Comparable<Student> {
        private String name;
        private int age;
        private double score;

        public Student(String name, int age, double score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", score=" + score +
                    '}';
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public double getScore() {
            return score;
        }

        public void setScore(double score) {
            this.score = score;
        }

        @Override
        public int compareTo(Student o) {
            if (this.score != o.score) {
                return -Double.compare(this.score, o.score);
            }
            return Integer.compare(this.age, o.age);
        }
    }

    /**
     * 2、已知数组存放一批 QQ 号码,长度 5-11 位，
     * String[] strs = {"10001","10086","12347806666","45612378901","10001","12347806666"}。
     * 将该数组里面的所有 qq 号都存放在 LinkedList 中，将 list 中重复元素删
     * 除，将 list 中所有元素分别用迭代器和增强 for 循环打印出来。
     */
    private static void removeRepeatElem(String[] strs) {
        List<String> list = new LinkedList<>();
        Set<String> set = new HashSet<>();
        for (String s : strs) {
            if (!set.contains(s)) {
                list.add(s);
                set.add(s);
            }
        }
        // 迭代器遍历
        iterator(list);
        System.out.println("-------------------");
        // forEach 遍历
        forEach(list);
    }

    private static void forEach(List<String> list) {
        for (String item : list) {
            System.out.println(item);
        }
    }

    private static void iterator(List<String> list) {
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    /**
     * 1、定义一个方法 listTest(ArrayList<String> list, String name)，要求返回
     * name 在 list 里面第一次出现的索引，如果 name 没出现过返回-1。
     */
    private static int listTest(ArrayList<String> list, String name) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(name)) {
                return i;
            }
        }
        return -1;
    }
}
